package com.alex.ioc.annotation.service;

import com.alex.ioc.annotation.dao.CarDao;
import com.alex.ioc.annotation.pojo.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CarService {

    // field 注入: 官方不推荐,但是实际上大家都在用
    @Autowired
    @Qualifier("carDao")
    private CarDao carDao1;

    // 构造器 注入: 强制性依赖
    private final CarDao carDao2;
    @Autowired
    public CarService(CarDao carDao) {
        this.carDao2 = carDao;
    }

    // setter 注入: 可变性依赖
    private CarDao carDao3;
    @Autowired
    public void setCarDao3(@Qualifier("car2Dao") CarDao carDao3) {
        this.carDao3 = carDao3;
    }

    @Autowired
    private List<CarDao> carDaoList;

    @Autowired
    private Map<String, CarDao> map;

    public void add(Car car) {
        System.out.println("service: add car ...");

        carDao1.save(car);
        carDao2.save(car);
        carDao3.save(car);
    }
}
